package com.hkd.user_service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkd.common.api.ApiResponse;
import com.hkd.user_service.api.SysMenuService;
import com.hkd.user_service.api.entity.SysMenu;
import com.hkd.user_service.mapper.SysMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author HKD
 * @date 2022/8/20 18:41
 */
@RestController
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Override
    @Transactional
    public ApiResponse<List<SysMenu>> initMenu() {
        String id = UUID.randomUUID().toString().replaceAll("-", "");
        SysMenu sysMenu = new SysMenu();
        sysMenu.setId(id);
        sysMenu.setName("系统管理");
        sysMenu.setLevel(1);
        sysMenu.setNodeType(1);
        sysMenu.setParentId("0");
        sysMenu.setPath("");
        sysMenu.setMenuCode("system_management");

        SysMenu userMenu = new SysMenu();
        userMenu.setName("用户管理");
        userMenu.setLevel(2);
        userMenu.setNodeType(2);
        userMenu.setParentId(sysMenu.getId());
        userMenu.setPath(sysMenu.getPath()+","+sysMenu.getId());
        userMenu.setMenuCode("user_management");

        SysMenu roleMenu = new SysMenu();
        roleMenu.setName("角色管理");
        roleMenu.setLevel(2);
        roleMenu.setNodeType(2);
        roleMenu.setParentId(sysMenu.getId());
        roleMenu.setPath(sysMenu.getPath()+","+sysMenu.getId());
        roleMenu.setMenuCode("role_management");

        SysMenu menu = new SysMenu();
        menu.setName("菜单管理");
        menu.setLevel(2);
        menu.setNodeType(2);
        menu.setParentId(sysMenu.getId());
        menu.setPath(sysMenu.getPath()+","+sysMenu.getId());
        menu.setMenuCode("menu_management");
        List<SysMenu> menuList=new ArrayList<>();
        menuList.add(sysMenu);
        menuList.add(userMenu);
        menuList.add(roleMenu);
        menuList.add(menu);
        this.saveBatch(menuList);
        return ApiResponse.ok(menuList);
    }

    @Override
    public ApiResponse addMenu(SysMenu sysMenu,String userId) {
        sysMenu.setCreateId(userId);
        sysMenu.setUpdateId(userId);
        this.save(sysMenu);
        return ApiResponse.ok();
    }

}
